package com.zdj.presentation.modules.im.views;

import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.zdj.presentation.R;
import com.zdj.presentation.base.BaseActivity;
import com.zdj.presentation.databinding.ActivityChatBinding;
import com.zdj.presentation.modules.im.handlers.ChatHandler;
import com.zdj.presentation.modules.im.vms.ChatVm;

public class ChatActivity extends BaseActivity<ChatVm,ActivityChatBinding> implements ChatHandler {

    private InputMethodManager inputMethodManager;
    private Handler mHander = new Handler();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mToolbar.setFitsSystemWindows(false);
        inputMethodManager = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
        viewModel.loadChatList();
    }

    @Override
    protected void initVms() {
        viewModel = new ChatVm(this);
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_chat;
    }

    @Override
    protected void setViewModel2Binding() {
        viewDatabinding.setChatVm(viewModel);
        viewDatabinding.setChatHandler(this);
    }

    @Override
    protected void initView() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setStackFromEnd(true);
        viewDatabinding.recycleViewChat.setLayoutManager(linearLayoutManager);
        viewDatabinding.recycleViewChat.setItemAnimator(new DefaultItemAnimator());
        /**
         * 设置下拉刷新的progress颜色 <br/>
         * modify by dengjie at 2016/01/06 13:31
         */
        viewDatabinding.recycleViewChat.setRefreshingColorResources(R.color.colorPrimary,
                R.color.colorPrimaryDark, R.color.colorPrimaryLight,
                R.color.colorAccent);
    }


    @Override
    public void clickFace(View view) {
        if(viewDatabinding.llFace.isShown()){
            showInputMethod();
        }else{
            hideInputMethod();
            viewDatabinding.llFace.setVisibility(View.VISIBLE);
        }

    }

    private void hideInputMethod() {
        inputMethodManager.hideSoftInputFromWindow(viewDatabinding.etContent.getWindowToken(), 0);
    }

    private void showInputMethod() {
        viewDatabinding.llFace.setVisibility(View.GONE);
        mHander.postDelayed(()->{
            inputMethodManager.showSoftInput(viewDatabinding.etContent, 0);
        },80);
    }
}
